﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FilmManagement2.Founction;
using System.Data;
using System.Data.SqlClient;
using System.IO;

namespace FilmManagement2.WebPage
{
    public partial class AdminPage : System.Web.UI.Page
    {
        public static int temp = 0;
        public static int temp_list = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            //StudioSelectedlbl.Text = "";
            if (Session["AdminName"]==null)
            {
                Response.Redirect("MainPage.aspx");
            }
            Label1.Text = Session["AdminName"].ToString();
            temp_list=0;
            ConnectToSql cts=new ConnectToSql();
            DataSet ds =new DataSet();
            ds=cts.GetFilm();
            if (!IsPostBack)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    FilmID.Items.Add(ds.Tables[0].Rows[i][0].ToString());
                    temp_list++;
                }
            }
            
        }

        protected void accountbut_Click(object sender, EventArgs e)
        {
            ClientScript.RegisterStartupScript(ClientScript.GetType(),"script", "<script> accountjava(); </script>");
            
            // Page.RegisterStartupScript( "script ", " <script> rbtclick(); </script> ") 
        }

        protected void filmbut_Click(object sender, EventArgs e)
        {
            ClientScript.RegisterStartupScript(ClientScript.GetType(), "script", "<script> filmjava(); </script>");
            
        }

        protected void studiobut_Click(object sender, EventArgs e)
        {
            ClientScript.RegisterStartupScript(ClientScript.GetType(), "script", "<script> studiojava(); </script>");
            ModifyStudioIdlbl.Visible = false;
            StudioSelectedlbl.Visible = false;
            DropDownList1.Visible = false;
            DropDownList2.Visible = false;
            DropDownList3.Visible = false;
            FilmNamelbl1.Visible = false;
            FilmNamelbl2.Visible = false;
            StartTimelbl.Visible = false;
            StartTimetxt.Visible = false;
            EndTimelbl.Visible = false;
            EndTimetxt.Visible = false;
            SeatStatus1.Visible = false;
            SeatStatus2.Visible = false;
            UpdateBut.Visible = false;
        }

        protected void subAccountBut_Click(object sender, EventArgs e)
        {
            ConnectToSql con = new ConnectToSql();
           
           if (txtAccountPS.Text != txtAccountPSCON.Text)
           {
               Response.Write("<script>alert('注册失败')</script>");

           }
           else
           {
               if (con.AddAdmin(txtAccountID.Text, txtAccountPS.Text))
               {
                   Response.Write("<script>alert('注册成功')</script>");
               }
               else
               {
                   Response.Write("<script>alert('此账号已存在')</script>");
               }
           }
        }

        protected void inputfilmsubbut_Click(object sender, EventArgs e)
        {
            bool b = false;
                string str = inputfilmpicturefileupload.FileName;
                string path= @"E:\毕业设计2\FilmManagement2\FilmManagement2\Picture\"+ str;
                string path2 = "";
                string type2 = str.Substring(str.LastIndexOf(".") + 1);
                type2=type2.ToUpper();
                ConnectToSql cts= new ConnectToSql();
              
                if (type2 == "BMP" || type2 == "GIF" || type2 == "JPEG" || type2 == "JPG" || type2 == "JIF")
                {
                    b = cts.AddFilm(inputfilmnametxt.Text, inputfilmleaderactortxt.Text, inputdetailstxt.Text, path,Convert.ToDecimal(inputprcetxt.Text));
                    inputfilmpicturefileupload.SaveAs(path);
                    if (b)
                    {
                      
                        
                        Response.Write("<script>alert('插入成功')</script>");
                    }
                    else
                    {
                        Response.Write("<script>alert('插入图片失败，请更换图片名')</script>");
                    }
                }
                else
                {
                    Response.Write("<script>alert('插入数据类型不匹配')</script>");
                }

                for (int i = 0; i < temp_list; i++)
                    FilmID.Items.RemoveAt(0);
        }

        protected void FilmID_SelectedIndexChanged(object sender, EventArgs e)
        {
            int filmId = Convert.ToInt32(FilmID.SelectedValue);
           
            DataSet ds = new DataSet();
            ConnectToSql cts = new ConnectToSql();
            ds=cts.GetFilmByID(filmId);
            updateselectfilmIDtxt.Text = ds.Tables[0].Rows[0][0].ToString();
            updatefilmnametxt.Text = ds.Tables[0].Rows[0][1].ToString();
            updateleaderactortxt.Text = ds.Tables[0].Rows[0][2].ToString();
            updatedetailtxt.Text = ds.Tables[0].Rows[0][3].ToString();
            updatepricetxt.Text = ds.Tables[0].Rows[0][5].ToString();
            for (int i = 0; i < temp_list; i++)
                FilmID.Items.RemoveAt(0);
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            bool b = false;
            string str = updatechangepicfileupload.FileName;
            string path = @"E:\毕业设计2\FilmManagement2\FilmManagement2\Picture\" + str;
            string path2 = "";
            string type2 = str.Substring(str.LastIndexOf(".") + 1);
            type2 = type2.ToUpper();
            ConnectToSql cts = new ConnectToSql();

            try
            {
                path2 = cts.GetPicturePathByID(Convert.ToInt32(updateselectfilmIDtxt.Text));
                if (updatechangepicfileupload.FileName == "")
                {

                    b = cts.UpdateFilm(Convert.ToInt32(updateselectfilmIDtxt.Text), updatefilmnametxt.Text, updateleaderactortxt.Text, updatedetailtxt.Text, Convert.ToDecimal(updatepricetxt.Text));
                    if (b)
                    {

                        Response.Write("<script>alert('修改成功')</script>");
                    }
                    else
                    {
                        Response.Write("<script>alert('修改失败')</script>");
                    }


                }
                else
                {
                    if (type2 == "BMP" || type2 == "GIF" || type2 == "JPEG" || type2 == "JPG" || type2 == "JIF")
                    {
                        b = cts.UpdateFilm(Convert.ToInt32(updateselectfilmIDtxt.Text), updatefilmnametxt.Text, updateleaderactortxt.Text, updatedetailtxt.Text, path, Convert.ToDecimal(updatepricetxt.Text));
                        updatechangepicfileupload.SaveAs(path);
                        if (b)
                        {
                            File.Delete(path2);
                            Response.Write("<script>alert('修改成功')</script>");
                        }
                        else
                        {
                            Response.Write("<script>alert('修改失败，请更换图片名')</script>");
                        }
                    }
                    else
                    {
                        Response.Write("<script>alert('修改的图片类型不匹配')</script>");
                    }
                }
            }
            catch 
            
            {
                Response.Write("<script>alert('插入类型不匹配')</script>");
            }
            
           
            for (int i = 0; i < temp_list; i++)
                FilmID.Items.RemoveAt(0);
            
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Response.Redirect("MainPage.aspx");
            
        }

        protected void StudioOneBut_Click(object sender, EventArgs e)
        {
            ClientScript.RegisterStartupScript(ClientScript.GetType(), "script", "<script> studiojava(); </script>");
            StudioSelectedlbl.Text = "StudioOne";
            ModifyStudioIdlbl.Visible = true;
            StudioSelectedlbl.Visible = true;
            DropDownList1.Visible = true;
            DropDownList2.Visible = false;
            DropDownList3.Visible = false;
            UpdateBut.Visible = false;
            StartTimetxt.Visible = false;
            EndTimetxt.Visible = false;
            SeatStatus1.Visible = false;
            SeatStatus2.Visible = false;
            FilmNamelbl1.Text = "";
            FilmNamelbl2.Text = "";
            StartTimelbl.Text = "";
 
            EndTimelbl.Text = "";

            SeatStatus1.Items.Clear();
            SeatStatus2.Items.Clear();
            
            
        
        }

        protected void StudioTwoBut_Click(object sender, EventArgs e)
        {
            ClientScript.RegisterStartupScript(ClientScript.GetType(), "script", "<script> studiojava(); </script>");
            StudioSelectedlbl.Text = "StudioTwo";
            ModifyStudioIdlbl.Visible = true;
            StudioSelectedlbl.Visible = true;
            DropDownList1.Visible =false;
            DropDownList2.Visible = true;
            DropDownList3.Visible = false;
            UpdateBut.Visible = false;
            StartTimetxt.Visible = false;
            EndTimetxt.Visible = false;
            SeatStatus1.Visible = false;
            SeatStatus2.Visible = false;
            FilmNamelbl1.Text = "";
            FilmNamelbl2.Text = "";
            StartTimelbl.Text = "";
            
            EndTimelbl.Text = "";
     
            SeatStatus1.Items.Clear();
            SeatStatus2.Items.Clear();
            
            
        }


        protected void StudioThreeBut_Click(object sender, EventArgs e)
        {
            ClientScript.RegisterStartupScript(ClientScript.GetType(), "script", "<script> studiojava(); </script>");
            StudioSelectedlbl.Text = "StudioThree";
            ModifyStudioIdlbl.Visible = true;
            StudioSelectedlbl.Visible = true;
            DropDownList1.Visible = false;
            DropDownList2.Visible = false;
            DropDownList3.Visible = true;
            UpdateBut.Visible = false;
            StartTimetxt.Visible = false;
            EndTimetxt.Visible = false;
            SeatStatus1.Visible = false;
            SeatStatus2.Visible = false;
            FilmNamelbl1.Text = "";
            FilmNamelbl2.Text = "";
            StartTimelbl.Text = "";

            EndTimelbl.Text = "";
           
            SeatStatus1.Items.Clear();
            SeatStatus2.Items.Clear();
          
          
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            FilmNamelbl1.Text = "FilmName:";
            StartTimelbl.Text = "StartTime:";            
            EndTimelbl.Text = "EndTime:";

            ClientScript.RegisterStartupScript(ClientScript.GetType(), "script", "<script> studiojava(); </script>");
            int studioid = Convert.ToInt32(DropDownList1.SelectedValue);
            ConnectToSql cts = new ConnectToSql();
            DataSet ds=cts.GetMovieStudioOneByStduioId(studioid);
              FilmNamelbl1.Visible = true;
            FilmNamelbl2.Visible = true;
            StartTimelbl.Visible = true;
            StartTimetxt.Visible = true;
            EndTimelbl.Visible = true;
            EndTimetxt.Visible = true;
            SeatStatus1.Visible = true;
            SeatStatus2.Visible = true;
            UpdateBut.Visible = true;
            int filmid = Convert.ToInt32(ds.Tables[0].Rows[0][1].ToString());
            DataSet ds2=cts.GetFilmByID(filmid);
            FilmNamelbl2.Text = ds2.Tables[0].Rows[0][1].ToString();
            StartTimetxt.Text = ds.Tables[0].Rows[0][2].ToString();
            EndTimetxt.Text = ds.Tables[0].Rows[0][3].ToString();
            string[] strarr={"AA","AB","AC","BA","BB","BC","CA","CB","CC"};
            foreach (string strarrtest in strarr)
            {
                SeatStatus1.Items.Add( strarrtest);
            }
            SeatStatus2.Items.Clear();
            SeatStatus2.Items.Add("True");
            SeatStatus2.Items.Add("False");
        }

        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            FilmNamelbl1.Text = "FilmName:";
            StartTimelbl.Text = "StartTime:";
            EndTimelbl.Text = "EndTime:";
           
            ClientScript.RegisterStartupScript(ClientScript.GetType(), "script", "<script> studiojava(); </script>");
            int studioid = Convert.ToInt32(DropDownList2.SelectedValue);
            ConnectToSql cts = new ConnectToSql();
            DataSet ds = cts.GetMovieStudioTwoByStduioId(studioid);
            FilmNamelbl1.Visible = true;
            FilmNamelbl2.Visible = true;
            StartTimelbl.Visible = true;
            StartTimetxt.Visible = true;
            EndTimelbl.Visible = true;
            EndTimetxt.Visible = true;
            SeatStatus1.Visible = true;
            SeatStatus2.Visible = true;
            UpdateBut.Visible = true;
            int filmid = Convert.ToInt32(ds.Tables[0].Rows[0][1].ToString());
            DataSet ds2 = cts.GetFilmByID(filmid);
            FilmNamelbl2.Text = ds2.Tables[0].Rows[0][1].ToString();
            StartTimetxt.Text = ds.Tables[0].Rows[0][2].ToString();
            EndTimetxt.Text = ds.Tables[0].Rows[0][3].ToString();
            string[] strarr = { "AA", "AB", "AC", "BA", "BB", "BC", "CA", "CB", "CC" };
            foreach (string strarrtest in strarr)
            {
                SeatStatus1.Items.Add(strarrtest);
            }
            SeatStatus2.Items.Clear();
            SeatStatus2.Items.Add("True");
            SeatStatus2.Items.Add("False");
        }

        protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
        {
            FilmNamelbl1.Text = "FilmName:";
            StartTimelbl.Text = "StartTime:";
            EndTimelbl.Text = "EndTime:";
            ClientScript.RegisterStartupScript(ClientScript.GetType(), "script", "<script> studiojava(); </script>");
            int studioid = Convert.ToInt32(DropDownList3.SelectedValue);
            ConnectToSql cts = new ConnectToSql();
            DataSet ds = cts.GetMovieStudioThreeByStduioId(studioid);
            FilmNamelbl1.Visible = true;
            FilmNamelbl2.Visible = true;
            StartTimelbl.Visible = true;
            StartTimetxt.Visible = true;
            EndTimelbl.Visible = true;
            EndTimetxt.Visible = true;
            SeatStatus1.Visible = true;
            SeatStatus2.Visible = true;
            UpdateBut.Visible = true;
            int filmid = Convert.ToInt32(ds.Tables[0].Rows[0][1].ToString());
            DataSet ds2 = cts.GetFilmByID(filmid);
            FilmNamelbl2.Text = ds2.Tables[0].Rows[0][1].ToString();
            StartTimetxt.Text = ds.Tables[0].Rows[0][2].ToString();
            EndTimetxt.Text = ds.Tables[0].Rows[0][3].ToString();
            string[] strarr = { "AA", "AB", "AC", "BA", "BB", "BC", "CA", "CB", "CC" };
            foreach (string strarrtest in strarr)
            {
                SeatStatus1.Items.Add(strarrtest);
            }
            SeatStatus2.Items.Clear();
            SeatStatus2.Items.Add("True");
            SeatStatus2.Items.Add("False");
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void UpdateBut_Click(object sender, EventArgs e)
        {
            try
            {
                string strd = "";
               
                DateTime StartTime = Convert.ToDateTime(StartTimetxt.Text);
                DateTime EndTime = Convert.ToDateTime(EndTimetxt.Text);
                string seatno = SeatStatus1.SelectedItem.ToString();
                bool seatstatus = Convert.ToBoolean(SeatStatus2.SelectedItem.ToString());
                ConnectToSql cts = new ConnectToSql();
                if (DropDownList1.Visible)
                {
                    int StudioId =Convert.ToInt32(DropDownList1.SelectedValue);
                    if (cts.UpdateStudioOne(StudioId, StartTime, EndTime, seatno, seatstatus))
                    {
                        Response.Write("<script>alert('修改成功')</script>");
                    }
                    else
                    {
                        Response.Write("<script>alert('请全部选择,并注意格式')</script>");
                    }
                }
                if (DropDownList2.Visible)
                {
                    int StudioId = Convert.ToInt32(DropDownList2.SelectedValue);
                    if (cts.UpdateStudioTwo(StudioId, StartTime, EndTime, seatno, seatstatus))
                    {
                        Response.Write("<script>alert('修改成功')</script>");
                    }
                    else
                    {
                        Response.Write("<script>alert('请全部选择,并注意格式')</script>");
                    }
                }
                if (DropDownList3.Visible)
                {
                    int StudioId = Convert.ToInt32(DropDownList3.SelectedValue);
                    if (cts.UpdateStudioThree(StudioId, StartTime, EndTime, seatno, seatstatus))
                    {
                        Response.Write("<script>alert('修改成功')</script>");
                    }
                    else
                    {
                        Response.Write("<script>alert('请输入选择,并注意格式')</script>");
                    }
                }
            }
            catch
            {
                Response.Write("<script>alert('请注意输入格式')</script>");
            }
            finally
            {
                ClientScript.RegisterStartupScript(ClientScript.GetType(), "script", "<script> studiojava(); </script>");
            }

        }

        protected void StudioAdd_Click(object sender, EventArgs e)
        {
            int filmid = 0;
            string timetemp = "";
            DateTime starttime=new DateTime();
                DateTime endtime=new DateTime();
           
                ClientScript.RegisterStartupScript(ClientScript.GetType(), "script", "<script> studiojava(); </script>");
                ConnectToSql cts = new ConnectToSql();
                if (StudioSelectedlbl.Text == "")
                {
                    Response.Write("<script>alert('请选择放映厅')</script>");
                }
                else
                {
                    try
                    {
                        filmid = Convert.ToInt32(DropDownList4.SelectedValue.ToString());
                        timetemp = "";
                        timetemp = AddStartTimetxt1.Text + " " + StarTimeDPH.SelectedItem.ToString() + ":" + StarTimeDPM.SelectedItem.ToString();
                        starttime = Convert.ToDateTime(timetemp);
                        timetemp = AddEndTimetxt1.Text + " " + EndTimeDPH.SelectedItem.ToString() + ":" + EndTimeDPM.SelectedItem.ToString();
                        endtime = Convert.ToDateTime(timetemp);
                        if (starttime < endtime)
                        {

                            if (StudioSelectedlbl.Text == "StudioOne")
                            {

                                cts.AddStudioOne(filmid, starttime, endtime);

                                Response.Write("<script>alert('加入成功')</script>");

                            }

                            if (StudioSelectedlbl.Text == "StudioTwo")
                            {
                                cts.AddStudioTwo(filmid, starttime, endtime);
                                Response.Write("<script>alert('加入成功')</script>");
                            }
                            if (StudioSelectedlbl.Text == "StudioThree")
                            {
                                cts.AddStudioThree(filmid, starttime, endtime);
                                Response.Write("<script>alert('加入成功')</script>");
                            }
                        }
                        else
                        {
                            Response.Write("<script>alert('StartTime请在EndTime之前')</script>");

                        }
                      
                    }
                    catch
                    {
                        Response.Write("<script>alert('请输入正确的格式')</script>");


                    }
                    
                }

            

        }

     
    }
}